package ssx.demo.bootdemofirst.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ssx.demo.bootdemofirst.dao.IDao;

import java.sql.Date;
import java.util.List;
import java.util.Map;

@RestController
public class ControllerA {

    @Autowired
    IDao iDao;

    @PostMapping("/hello")
    public String fu(){
        return "hello world";
    }

    @RequestMapping("/add")
    public String f(@RequestBody String req){
        try {
            Map map = new ObjectMapper().readValue(req, Map.class);
            java.util.Date date1 = new java.util.Date();
            Date date = new Date(date1.getTime());
            iDao.add(map.get("name").toString(), date1);
            return "OK add";
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "NO add";
    }

    @RequestMapping("/query")
    public String ff(){
        List query = iDao.query();
        return query.toString();
    }
}
